package com.gragra.sampling.binary.tests;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import com.gragra.sampling.ThreadedUniformAccess;
import com.gragra.sampling.binary.BinaryStructure;
import com.gragra.sampling.binary.ConstSpanConverterFactory;
import com.gragra.sampling.binary.FixedConstAssigner;
import com.gragra.sampling.binary.GeneratorForBinaryGibbs;
/**
 * 
 * @author Christoph Teichmann
 * 21.09.2013
 */
public class TestFixedConstAssigner
{
	/**
	 * 
	 */
	private BinaryStructure bs;
	/**
	 * 
	 */
	private FixedConstAssigner fca;
	@Before
	public void setUp() throws Exception
	{
		bs = (new GeneratorForBinaryGibbs(false, new ConstSpanConverterFactory())).makeBinaryStructure("this|0 is|1 a|2 simple|3 sentence|4",5,0.0);
		ArrayList<String> al = new ArrayList<String>();
		al.add("0;2 0;3 3;5 0;5");
		al.add("1;3 0;3 0;4 0;5");
		fca = new FixedConstAssigner(5, al, 3.0, 5);
		bs.initialize(fca, new ThreadedUniformAccess(5));
		bs.subsume(0, 1, 5, 0);
		bs.subsume(5, 2, 6, 0);
		bs.subsume(6, 3, 7, 0);
		bs.subsume(7, 4, 8, 0);
	}
	@Test
	public void testMakeLogLike()
	{
		double d = fca.makeLogProb(bs, 0);
		assertEquals(d,9.0,0.000001);
		bs.subsume(1, 2, 5, 0);
		bs.subsume(0, 5, 6, 0);
		bs.subsume(6, 3, 7, 0);
		bs.subsume(7, 4, 8, 0);
		d = fca.makeLogProb(bs, 0);
		assertEquals(d,12.0,0.000001);
		assertEquals(fca.getNumberOfEvaluations(),8.0,0.00001);
		d = fca.getTransitionLogLike(null, 0, 0, 2, 5);
		assertEquals(d,3.0,0.0000001);
		assertEquals(fca.getNumberOfAuxEvals(),1.0,0.00001);
		ThreadedUniformAccess tua = new ThreadedUniformAccess(5);
		for(int i=0;i<8000;++i)
		{
			for(int thread=0;thread<5;++thread)
			{bs.resample(fca, 1.0, thread, tua);}
			bs.addSample();
		}
		assertEquals(bs.getNumberSeen("0;5 0;4 0;3 1;3")/bs.getNumberSeen("0;5 0;3 0;2 3;5"),1.0,0.2);
	}
}